﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class ConsumptionPeriodsController : Controller
    {
        private ConsumptionPeriodsInfo fundinfo = new ConsumptionPeriodsInfo(true);
        public ActionResult ConsumptionPeriodsIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取消费时段列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetConsumptionPeriodsList(PageVM pvm)
        {
            string query = " and 1=1 ";
            IList<ConsumptionPeriodsInfo> fundlist = fundinfo.SelConsumptionPeriodsInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = fundinfo._ConsumptionPeriodscount;
            List<string> v;
            foreach (ConsumptionPeriodsInfo item in fundlist)
            {

                v = new List<string>();
                v.Add(item.PeriodCode.ToString());
                v.Add(item.PeriodName);
                v.Add(item.BeginTime);
                v.Add(item.EndTime);
                v.Add(item.NextDay);
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.PeriodCode.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditConsumptionPeriods(string ID)
        {
            ConsumptionPeriodsVM vm = new ConsumptionPeriodsVM();
            ConsumptionPeriodsInfo info = new ConsumptionPeriodsInfo();
            fundinfo = info.GetConsumptionPeriodsInfoOne(ID);
            vm.PeriodCode = fundinfo.PeriodCode;
            vm.PeriodName = fundinfo.PeriodName;
            vm.BeginTime = fundinfo.BeginTime;
            vm.EndTime = fundinfo.EndTime;
            vm.NextDay = fundinfo.NextDay;
            if (fundinfo.NextDay == "True")
            {
                vm.blNextDay = true;
            }
            else
            {
                vm.blNextDay = false;
            }
            return View(vm);
        }

        /// <summary>
        /// 保存增减款类别（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveConsumptionPeriodsEdit(ConsumptionPeriodsVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                ConsumptionPeriodsInfo fund = new ConsumptionPeriodsInfo()
                {
                    PeriodCode = model.PeriodCode,
                    BeginTime = model.BeginTime,
                    EndTime = model.EndTime,
                    blNextDay = model.blNextDay
                };
                result = fundinfo.SaveConsumptionPeriodsEdit(fund);
            }
            return Json(result);
        }
    }
}
